#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFontDialog>
#include <QFileDialog>
#include <QStatusBar>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("记事本");
    QMenuBar* menuBar=this->menuBar();
    QMenu* menu1=new QMenu("文件");
    QMenu* menu2=new QMenu("字体");
    menuBar->addMenu(menu1);
    menuBar->addMenu(menu2);
    QAction* act1=new QAction("打开");
    QAction* act2=new QAction("保存");
    QAction* act3=new QAction("设置");
    QAction* act4=new QAction("获取属性");
    menu1->addAction(act1);
    menu1->addAction(act2);
    menu2->addAction(act3);
    menu1->addAction(act4);
    edit=new QTextEdit(this);
    this->setCentralWidget(edit);

    connect(act1,&QAction::triggered,this,&MainWindow::handleact1);
    connect(act2,&QAction::triggered,this,&MainWindow::handleact2);
    connect(act3,&QAction::triggered,this,&MainWindow::handleact3);
    connect(act4,&QAction::triggered,this,&MainWindow::handleact4);

}

void MainWindow::handleact3()
{
    bool ret;
    QFont font=QFontDialog::getFont(&ret);
    edit->setFont(font);
}

void MainWindow::handleact1()
{
    QString path=QFileDialog::getOpenFileName();

    ui->statusbar->showMessage(path);
    QFile file(path);
    bool ret=file.open(QFile::ReadOnly);
    if(ret==false)
        ui->statusbar->showMessage(path+"打开失败");

    QString content=file.readAll();

    file.close();

    edit->setText(content);

}

void MainWindow::handleact2()
{
    QString path=QFileDialog::getSaveFileName();
    ui->statusbar->showMessage(path);
    QFile file(path);
    bool ret=file.open(QFile::WriteOnly);
    if(ret==false)
        ui->statusbar->showMessage(path+"打开失败");

    QString content=edit->toPlainText();

    file.write(content.toUtf8());
    file.close();

}

void MainWindow::handleact4()
{
    QString path=QFileDialog::getOpenFileName();
    QFileInfo info(path);

    QString text="文件名："+info.fileName();
    text+="文件大小："+QString::number(info.size());
    text+="文件路径："+info.path();
    edit->setText(text);
}

MainWindow::~MainWindow()
{
    delete ui;
}

